package MyFrame;

import dao.Player;
import tools.MusicPlayer;

import javax.imageio.ImageIO;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class CaiYuFrame extends JFrame implements MouseListener {
    private int height=584;
    private int width=405;
    private int flag=0;
    public CaiYuFrame(Player player) throws UnsupportedAudioFileException, IOException, LineUnavailableException
    {
        this.setVisible(true);
        this.setResizable(false);
        this.setSize(width,height);
        int ScreenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度
        int ScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度
        this.setLocation((ScreenWidth - width) / 2, (ScreenHeight - height) / 2);//设置窗口显示位置,由屏幕大小和窗口大小计算出中央坐标
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.addMouseListener((MouseListener)this);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        BufferedImage CaiYu0= null,CaiYu1=null,CaiYu2=null;
        try {
            CaiYu0 = ImageIO.read(new File("images/caiyu0.jpg"));
            CaiYu1 =ImageIO.read(new File("images/caiyu1.jpg"));
            CaiYu2 =ImageIO.read(new File("images/caiyu2.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(flag==0)
        g.drawImage(CaiYu0,10,20,this);
        else if(flag==1) {
            g.drawImage(CaiYu1, 10, 20, this);
        }
        else if(flag==2) {
            g.drawImage(CaiYu2, 10, 20, this);
            MusicPlayer mp=new MusicPlayer("music/jc.wav",true);
            Thread t=new Thread(mp);
            t.start();
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
     int x,y;
     x=e.getX();
     y=e.getY();
     System.out.println("x:"+x+" Y:"+y);
     if(x>=28&&x<121&&y>=323&&y<=359)
     {
         MusicPlayer mp=new MusicPlayer("music/botton.wav",false);
         Thread t2=new Thread(mp);
         t2.start();
         flag=1;
         repaint();
         int result=JOptionPane.showConfirmDialog(this,"纳尼？要做羞羞的事情吗？前辈~");
         if(result==0)
             flag=2;
         repaint();
     }
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
